<?php
class Product extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('product_model');
		$this->load->model('images_model');
		$this->load->model('large_images_model');
		$this->load->model('live_view_model');
	}
	
	public function index(){
	}
	
	protected $breadcrumb;
	
	public function get($id,$filter_string = ''){
		$e = new stdClass();
		$f = new stdClass();
		$g = new stdClass();
		$h = new stdClass();
		$i = new stdClass();
		$d = new stdClass();
		$colors_order = array();
		$i->colour = "I";
		$colors_order[] = $i;
		$h->colour = "H";
		$colors_order[] = $h;
		$g->colour = "G";
		$colors_order[] = $g;
		$f->colour = "F";
		$colors_order[] = $f;
		$e->colour = "E";
		$colors_order[] = $e;
		$d->colour = "D";
		$colors_order[] = $d;

		$data = array();
		$filter = array();
		
		$temp = explode('-',$id);
		$id = $temp[0];
		
		if (strlen($filter_string)){
			$pre_filter = explode(';',($filter_string));

			foreach ($pre_filter as $key=>$value){
				$filter_tags = explode("=",$value);
				$filter[$filter_tags[0]] = $filter_tags[1];
			}
	
		}
		
		if (!($this->session->userdata('product_id_live') && $this->session->userdata('product_id_live') == $id)){
			$this->session->set_userdata(array('product_id_live' => $id));
			
			$liveModel = new live_view_model();
			$liveModel->product_id = $id;
			$liveModel->date = date('Y-m-d H:i:s');
			$liveModel->ip = $_SERVER['REMOTE_ADDR'];
			
			$liveModel->insert();
		}

		$searchSession = ($this->session->userdata('product_search') ? $this->session->userdata('product_search') : array());
		$searchStringSession = ($this->session->userdata('product_search_url') ? $this->session->userdata('product_search_url') : '');
		
		$products = $this->product_model->getProductList($searchSession)->result();
		
		$idx = 0;
		foreach ($products as $idx=>$row){
			if ($row->id == $id){
				break;
			}
		}
		
		if (isset($products[$idx-1])){
			$data['previousProd'] = $products[$idx-1];
		}
		
		if (isset($products[$idx+1])){
			$data['nextProd'] = $products[$idx+1];
		}
		
		$data['backString'] = $searchStringSession;
		
		$data['colors'] = $this->product_model->getColorsByProductId($id)->result();
		$data['clarities'] = $this->product_model->getClaritiesByProductId($id)->result();
		
		foreach ($colors_order as $key=>$row){
			if (!in_array($row,$data['colors'])){
				unset($colors_order[$key]);
			}
		}
	
		$data['images'] = $this->images_model->get($id)->result();
		$data['large_images'] = $this->large_images_model->get($id)->result();
		
		$data['colors'] = $colors_order;

		if (!empty($data['colors'])){
			$currentColor = (isset($filter['color']) ? $filter['color'] : array_shift(array_values($data['colors']))->colour);
		}

		if (isset($currentColor) && !empty($data['clarities'])){
			foreach ($data['clarities'] as $key=>$value){
				$checkProd = $this->product_model->getAll(array('parent_id' => $id, 'color' => $currentColor, 'clarity' => $value->clarity))->result();
				if (empty($checkProd)){
					if (isset($filter['clarity']) && $filter['clarity'] == $data['clarities'][$key]->clarity){
						unset($filter['clarity']);
					}
					unset($data['clarities'][$key]);
				}
			}
		}
		if (!empty($data['clarities'])){
			$temp = array_shift(array_values($data['clarities']));
			$filter['clarity'] = (isset($filter['clarity']) ? $filter['clarity'] : $temp->clarity);
		}

		
		$session_country = $this->session->userdata('selected_country');
		$country = $session_country ? $session_country : $this->config->item('def_country');
		
		switch ($country){
			case 1:
				$field = 'size_uk';
				break;
			case 6:
			case 43:
				$field = 'size_usa';
				break;
			case 7:
			case 21:
				$field = 'size_japan';
				break;
			default:
				$field = 'size_eu';
				break;
		}
		
		$data['field'] = $field;
		$data['prodObj'] = new Product_model();
				
		if (empty($filter)){
			if (!empty($data['colors'])){
				$filter['color'] = array_shift(array_values($data['colors']));
				$filter['color'] = $filter['color']->colour;
			}
			
			if (!empty($data['clarities'])){
				$filter['clarity'] = array_shift(array_values($data['clarities']));
				$filter['clarity'] = $filter['clarity']->clarity;
			}
		}
		
		$filter['parent_id'] = $id;
		$data['main_product'] = $this->product_model->getAll(array('id' => $id))->result();
		
		$data['child_products'] = $this->product_model->getAll($filter)->result();

		$data['filter'] = $filter;
		
		$data['related_products'] = $this->product_model->getRelatedProducts($id)->result();
		
		$data['hide_promotions'] = 1;
		
		$data['current_url'] = current_url();
	
		preg_match('/([a-zA-Z]*)([0-9]*)/', $data['main_product'][0]->sku,$skuMatch);
		
		if (!empty($skuMatch)){
			$skuRoot = $skuMatch[0];
			$data['metals'] = $this->product_model->getMetlasByProductSku($skuRoot)->result();
			$data['skuRoot'] = $skuRoot;
		} else {
			$data['metals'] = $this->product_model->getMetlasByProductId($id)->result();
		}
		
		$data['breadcrumbs'] = array();
		$mainCategory = $this->product_model->getProductCategory(array('product_id' => $id, 'category_id' => 0))->result();
		
		if (!empty($mainCategory)){
			$this->getCategories($id,$mainCategory[0]->id);
			$this->breadcrumb[$mainCategory[0]->id] = $mainCategory[0]->name;
			$data['breadcrumbs'] = array_reverse($this->breadcrumb,true);
		}
		
		$this->template->add_js('application/assets/js/swfobject.js');
		$this->template->add_js('application/assets/js/jqueryPlugins/jquery.jqprint-0.3.js');
		$this->template->add_js('application/assets/js/jquery-ui-1.8.21/jquery-ui-1.8.21.custom.min.js');
		$this->template->add_js('application/assets/js/jqueryPlugins/jCarousel/lib/jquery.jcarousel.js');
		$this->template->add_css('application/assets/js/jqueryPlugins/jCarousel/skins/tango/skin.css');
		$this->template->add_js('application/assets/js/shadowbox.js');
		$this->template->add_css('application/assets/css/shadowbox.css');
		$this->template->add_js('application/assets/js/jqueryPlugins/Rating/jquery.raty.js');
		
		$this->template->add_js('application/assets/js/product_details.js');
		$this->template->write('pageTitle', $data['main_product'][0]->name);
		$this->template->write_view('content','front/details.php',$data);
		$this->template->write_view('footer','templates/front/footer.php',$data,true);
		$this->template->render();
	}
	
	public function get_url($value_color,$value_clarity){
		$current_url = $this->input->post('current_url');
		
		$segment_array = explode('/',$current_url);
		$filter_name = 'color';
		$new_value = $value_color;
	
		if (isset($segment_array[8])){
			if (strpos($segment_array[8], $filter_name) !== FALSE){
				$segment_element = explode(";",$segment_array[8]);
				
				foreach ($segment_element as $idx=>$element){
					if (strpos($element, $filter_name) !== FALSE){
						if ($element != $filter_name . '=' . $new_value){
							if ($new_value){
								$segment_element[$idx] = substr($element, 0, strpos($element, "="));
								$segment_element[$idx] .= '=' .$new_value;
							} else {
								unset($segment_element[$idx]);
							}
						}
					}
				}
				$segment_array[8] = join(';', $segment_element);				
			} else {
				
				if ($new_value){
					$segment_array[8] .= ';' . $filter_name . "=" . $new_value;
				}
			}
		} else {
			if ($new_value){
				$segment_array[] = $filter_name . "=" . $new_value;
			}
		}
		
		$filter_name = 'clarity';
		$new_value = $value_clarity;
		if (isset($segment_array[8])){
			if (strpos($segment_array[8], $filter_name) !== FALSE){
				$segment_element = explode(";",$segment_array[8]);
				
				foreach ($segment_element as $idx=>$element){
					if (strpos($element, $filter_name) !== FALSE){
						if ($element != $filter_name . '=' . $new_value){
							if ($new_value){
								$segment_element[$idx] = substr($element, 0, strpos($element, "="));
								$segment_element[$idx] .= '=' .$new_value;
							} else {
								unset($segment_element[$idx]);
							}
						}
					}
				}
				$segment_array[8] = join(';', $segment_element);				
			} else {
				
				if ($new_value){
					$segment_array[8] .= ';' . $filter_name . "=" . $new_value;
				}
			}
		} else {
			if ($new_value){
				$segment_array[] = $filter_name . "=" . $new_value;
			}
		}
		echo json_encode(array('status' => 1, 'url' => join('/',$segment_array)));
	}
	
	function get_url_metal(){
		$metal = $this->input->post('metal');
		$skuRoot = $this->input->post('skuRoot');
		
		$prod = $this->product_model->getAll(array('language_id' => $this->config->item('lang_obj')->id, 'sku_like' => $skuRoot, 'metal' => $metal, 'parent_id' => 0))->result();
		$url = get_product_url($prod[0]->name,$prod[0]->id);
		
		echo json_encode(array('status' => 1, 'url' => $url));
	}
	
	function add_rate(){
		$product_id = $this->input->post('product_id');
		$rating = $this->input->post('rating');
		
		$this->product_model->addRating($product_id,$rating);
	}
	
	function getCategories($prod_id,$cat_id){
		$category = $this->product_model->getProductCategory(array('product_id' => $prod_id, 'category_id' => $cat_id))->result();
		
		if (!empty($category)){
			$this->getCategories($prod_id,$category[0]->id);
			$this->breadcrumb[$category[0]->id] = $category[0]->name;
		} else {
			return;
		}
	}
	
	function a($prod_id,$cat_id){
		
	}
}